home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
EnigmA Amiga Run 1996 June
/
EnigmA AMIGA RUN 08 (1996)(G.R. Edizioni)(IT)[!][issue 1996-06][EARSAN CD VII].iso
/
earcd
/
cmdity
/
grabky11.lha
/
GrabKey
/
GrabKey.c
< prev
next >
Wrap
C/C++ Source or Header
|
1996-05-08
|
20KB
|
716 lines
/*
** $VER: GrabKey 1.0 (08 May 1996) **
**
** © 1996 Timo C. Nentwig
** all rights reserved !
**
** ======================================
**
** Language:
** ¯¯¯¯¯¯¯¯
**
** Program is compiled by SAS/C
**
**
** Purpose:
** ¯¯¯¯¯¯¯
**
** Grab/Print active window or screen
** by hotkey.
**
** Requirements:
** ¯¯¯¯¯¯¯¯¯¯¯¯
**
** · AmigaOS v39+
** · datatypes.library
**
** Bugs:
** ¯¯¯¯
**
**
**
** ToDo:
** ¯¯¯¯
**
**
**
** Notes:
** ¯¯¯¯¯
**
**
** ======================================
**
** History:
** ¯¯¯¯¯¯¯
**
** 08 May 1996 - 1.0 : initial release
** 08 May 1996 - 1.1 : can also grab active screen
** OUTPATH -> FILE
**
*/
/// #include
#include <datatypes/pictureclass.h>
#include <dos/dos.h>
#include <exec/types.h>
#include <exec/memory.h>
#include <graphics/gfxbase.h>
#include <intuition/intuition.h>
#include <intuition/intuitionbase.h>
#include <libraries/commodities.h>
#include <proto/commodities.h>
#include <proto/datatypes.h>
#include <proto/dos.h>
#include <proto/exec.h>
#include <proto/graphics.h>
#include <proto/intuition.h>
#include <stdio.h>
#include <strings.h>
///
/// #define
#define EVT_WINKEY 1L
#define EVT_SCRKEY 2L
#define EVT_PRTKEY 3L
#define EVT_PRTKEY_SCR 4L
#define PRG_VERSION "1.1"
#define PRG_TITLE "GrabKey"
#define PRG_AUTHOR "Timo C. Nentwig"
#define PRG_YEAR "1996"
#define MAX(a,b) ((a) > (b) ? (a) : (b))
///
/// Prototypes
VOID ProcessMsg (VOID);
BOOL AttachFilter (STRPTR onkey, ULONG user_event);
VOID SavePicture (Object *Picture, STRPTR FileName);
VOID PrintPicture (Object *Picture);
Object * GetPicture (BOOL fullscreen);
///
struct IntuitionBase *IntuitionBase;
struct GfxBase *GfxBase;
struct Library *DataTypesBase;
struct Library *CxBase;
struct MsgPort *broker_mp;
CxObj *broker;
ULONG cxsigflag;
STRPTR filename;
struct Picture
{
UWORD Left;
UWORD Top;
UWORD Width;
UWORD Height;
UWORD PageWidth;
UWORD PageHeight;
UWORD Depth;
};
/// main ()
VOID
main (UWORD argc, STRPTR *argv)
{
if (IntuitionBase = (struct IntuitionBase *) OpenLibrary ("intuition.library", 39))
{
if (DOSBase = (struct DosLibrary *) OpenLibrary ("dos.library", 39))
{
if (GfxBase = (struct GfxBase *) OpenLibrary ("graphics.library", 39))
{
if (DataTypesBase = OpenLibrary ("datatypes.library", 39))
{
if (CxBase = OpenLibrary ("commodities.library", 39))
{
if (broker_mp = CreateMsgPort())
{
STRPTR *ttypes;
STRPTR winkey;
STRPTR scrkey;
STRPTR prtkey;
STRPTR prtkey_scr;
struct NewBroker newbroker =
{
NB_VERSION,
PRG_TITLE,
PRG_TITLE" "PRG_VERSION" © "PRG_YEAR" by "PRG_AUTHOR,
"Save/Print active window by hotkey",
NBU_UNIQUE | NBU_NOTIFY,
0, 0, 0, 0
};
newbroker.nb_Port = broker_mp;
cxsigflag = 1L << broker_mp->mp_SigBit;
ttypes = ArgArrayInit (argc, argv);
newbroker.nb_Pri = (BYTE) ArgInt (ttypes, "CX_PRIORITY", 0);
winkey = ArgString (ttypes, "WINKEY", "LALT F1");
scrkey = ArgString (ttypes, "SCRKEY", "LALT F2");
prtkey = ArgString (ttypes, "PRTKEY", "LALT F3");
prtkey_scr = ArgString (ttypes, "PRTKEY_SCR", "LALT F4");
filename = ArgString (ttypes, "FILE", "ram:"PRG_TITLE".IFF");
if (broker = CxBroker (&newbroker, NULL))
{
CxMsg *msg;
if (AttachFilter (winkey, EVT_WINKEY) &&
AttachFilter (scrkey, EVT_SCRKEY) &&
AttachFilter (prtkey, EVT_PRTKEY) &&
AttachFilter (prtkey, EVT_PRTKEY_SCR))
{
ActivateCxObj (broker, 1L);
ProcessMsg();
}
DeleteCxObjAll (broker);
while (msg = (CxMsg *) GetMsg (broker_mp)) // Empty the port of all CxMsgs
ReplyMsg ((struct Message *) msg);
}
DeletePort(broker_mp);
}
ArgArrayDone();
CloseLibrary (CxBase);
}
else
{
printf ("ERROR: Couldn't open commodities.library v39+\n");
}
CloseLibrary ((struct Library *) DataTypesBase);
}
else
{
printf ("ERROR: Couldn't open datatypes.library v39+\n");
}
CloseLibrary ((struct Library *) GfxBase);
}
else
{
printf ("ERROR: Couldn't open graphics.library v39+\n");
}
CloseLibrary ((struct Library *) DOSBase);
}
CloseLibrary ((struct Library *) IntuitionBase);
}
else
{
printf ("ERROR: Couldn't open intuition.library v39+\n");
}
}
///
/// ProcessMsg ()
/*
* FROM /rkm/libs/commodities/hotkey.c
*
* FUNCTION Process commodity messages.
*
* NOTE
*
* EXAMPLE ProcessMsg ();
*
*/
VOID
ProcessMsg (VOID)
{
CxMsg *msg;
Object *Picture;
ULONG sigrcvd;
ULONG msgid;
ULONG msgtype;
LONG returnvalue = 1L;
while (returnvalue)
{
sigrcvd = Wait (SIGBREAKF_CTRL_C | cxsigflag);
while (msg = (CxMsg *) GetMsg (broker_mp))
{
msgid = CxMsgID (msg);
msgtype = CxMsgType (msg);
ReplyMsg ((struct Message *) msg);
switch (msgtype)
{
case CXM_IEVENT:
switch (msgid)
{
case EVT_WINKEY: // window
if (Picture = GetPicture (FALSE))
{
SavePicture (Picture, filename);
}
break;
case EVT_SCRKEY: // screen
if (Picture = GetPicture (TRUE))
{
SavePicture (Picture, filename);
}
break;
case EVT_PRTKEY: // window
if (Picture = GetPicture (FALSE))
{
PrintPicture (Picture);
}
break;
case EVT_PRTKEY_SCR: // screen
if (Picture = GetPicture (TRUE))
{
PrintPicture (Picture);
}
break;
}
break;
case CXM_COMMAND:
switch (msgid)
{
case CXCMD_DISABLE:
ActivateCxObj (broker, 0L);
break;
case CXCMD_ENABLE:
ActivateCxObj (broker, 1L);
break;
case CXCMD_KILL:
returnvalue = 0L;
break;
case CXCMD_UNIQUE:
returnvalue = 0L;
break;
}
break;
}
}
if (sigrcvd & SIGBREAKF_CTRL_C)
returnvalue = 0L;
}
}
///
/// AttachFilter ()
/*
* FROM /CenterWin/CenterWin.c
*
* FUNCTION Attach a filter.
*
* NOTE
*
* EXAMPLE AttachFilter (hotkey, EVT_HOTKEY);
*
*/
BOOL
AttachFilter (STRPTR onkey, ULONG user_event)
{
CxObj *filter;
CxObj *sender;
CxObj *translator;
if (filter = CxFilter (onkey))
{
AttachCxObj (broker, filter);
if (sender = CxSender (broker_mp, user_event))
{
AttachCxObj (filter, sender);
if (translator = CxTranslate (NULL))
{
AttachCxObj (filter, translator);
if ( ! (CxObjError (filter)))
return (TRUE);
}
}
}
return (FALSE);
}
///
/// SavePicture ()
/*
* FUNCTION Save an Object *Picture.
*
* NOTE
*
* EXAMPLE SavePicture (Picture, "ram:file");
*
*/
VOID
SavePicture (Object *Picture, STRPTR FileName)
{
BPTR FileHandle;
if (FileHandle = Open (FileName, MODE_NEWFILE))
{
if (DoMethod (Picture, DTM_WRITE, NULL, FileHandle, DTWM_IFF, NULL))
{
Close (FileHandle);
}
else
{
Close (FileHandle);
DeleteFile (FileName);
}
}
DisposeDTObject (Picture);
}
///
/// PrintPicture ()
/*
* FUNCTION Print an Object *Picture.
*
* NOTE
*
* EXAMPLE PrintPicture (Picture);
*
*/
VOID
PrintPicture (Object *Picture)
{
struct MsgPort *PrinterPort;
if (PrinterPort = CreateMsgPort())
{
union printerIO *PrinterIO;
if (PrinterIO = (union printerIO *) CreateIORequest (PrinterPort, sizeof (union printerIO)))
{
if ( ! (OpenDevice ("printer.device", 0, (struct IORequest *) PrinterIO, NULL)))
{
DoMethod (Picture, DTM_PRINT, NULL, PrinterIO, NULL);
CloseDevice ((struct IORequest *) PrinterIO);
}
DeleteIORequest ((struct IORequest *) PrinterIO);
}
DeleteMsgPort (PrinterPort);
}
DisposeDTObject (Picture);
}
///
/// GetPicture ()
/*
* FUNCTION Get bitmap for picture.
*
* NOTE
*
* EXAMPLE Object *Picture = GetPicture();
*
*/
Object *
GetPicture (BOOL fullscreen)
{
struct RastPort *RPort;
if (RPort = (struct RastPort *) AllocVec (sizeof (struct RastPort), MEMF_ANY))
{
struct Picture *Pic;
if (Pic = AllocVec (sizeof (struct Picture), MEMF_ANY))
{
struct BitMap *BitMap;
struct Window *Win;
struct Screen *Scr;
ULONG IntuiLock;
BOOL Locked = TRUE;
InitRastPort (RPort);
IntuiLock = LockIBase (NULL);
Win = IntuitionBase -> ActiveWindow;
Scr = IntuitionBase -> ActiveScreen;
Pic -> PageWidth = Scr -> Width;
Pic -> PageHeight = Scr -> Height;
if (fullscreen)
{
Pic -> Left = Scr -> LeftEdge;
Pic -> Top = Scr -> TopEdge;
Pic -> Width = Scr -> Width;
Pic -> Height = Scr -> Height;
Pic -> Depth = GetBitMapAttr (Scr -> RastPort . BitMap, BMA_DEPTH);
BitMap = AllocBitMap (Pic -> Width, Pic -> Height, Pic -> Depth, BMF_CLEAR, Scr -> RastPort . BitMap);
}
else
{
Pic -> Left = Win -> LeftEdge;
Pic -> Top = Win -> TopEdge;
Pic -> Width = Win -> Width;
Pic -> Height = Win -> Height;
Pic -> Depth = GetBitMapAttr (Win -> RPort -> BitMap, BMA_DEPTH);
BitMap = AllocBitMap (Pic -> Width, Pic -> Height, Pic -> Depth, BMF_CLEAR, Win -> RPort -> BitMap);
}
if (BitMap)
{
ULONG *ColourTable;
ULONG ModeID;
LONG NumColours;
NumColours = Scr -> ViewPort . ColorMap -> Count;
ModeID = BestModeID (BIDTAG_NominalWidth, Pic -> Width,
BIDTAG_NominalHeight, Pic -> Height,
BIDTAG_Depth, Pic -> Depth,
TAG_END);
RPort -> BitMap = BitMap;
if (fullscreen)
ClipBlit (&Scr -> RastPort, 0, 0, RPort, 0, 0, Pic -> Width, Pic -> Height, 0xC0);
else
ClipBlit (Win -> RPort, 0, 0, RPort, 0, 0, Pic -> Width, Pic -> Height, 0xC0);
WaitBlit ();
if (ColourTable = (ULONG *) AllocVec (sizeof (ULONG) * 3 * NumColours, MEMF_ANY))
{
Object *Picture;
GetRGB32 (Scr -> ViewPort . ColorMap, 0, NumColours, ColourTable);
UnlockIBase (IntuiLock);
Locked = FALSE;
if (Picture = NewDTObject (PRG_TITLE"BitMap", DTA_SourceType, DTST_RAM,
DTA_GroupID, GID_PICTURE,
PDTA_NumColors, NumColours,
PDTA_BitMap, BitMap,
PDTA_ModeID, ModeID,
TAG_END))
{
struct ColorRegister *ColourMap;
struct BitMapHeader *BitMapHeader;
ULONG *Colours;
if (GetDTAttrs (Picture, PDTA_BitMapHeader, &BitMapHeader,
PDTA_ColorRegisters, &ColourMap,
PDTA_CRegs, &Colours,
TAG_END) == 3)
{
BitMapHeader -> bmh_Left = Pic -> Left;
BitMapHeader -> bmh_Top = Pic -> Top;
BitMapHeader -> bmh_Width = Pic -> Width;
BitMapHeader -> bmh_Height = Pic -> Height;
BitMapHeader -> bmh_Depth = Pic -> Depth;
BitMapHeader -> bmh_PageWidth = Pic -> PageWidth;
BitMapHeader -> bmh_PageHeight = Pic -> PageHeight;
CopyMem (ColourTable, Colours, 3 * sizeof (ULONG) * NumColours);
while (NumColours--)
{
ColourMap -> red = (UBYTE) ((*Colours++) >> 24);
ColourMap -> green = (UBYTE) ((*Colours++) >> 24);
ColourMap -> blue = (UBYTE) ((*Colours++) >> 24);
ColourMap++;
}
FreeVec (ColourTable);
FreeVec (RPort);
return (Picture);
}
else
{
printf ("ERROR: GetDTAttrs() failed.\n");
}
DisposeDTObject (Picture);
BitMap = NULL;
}
else
{
printf ("ERROR: NewDTObject failed.\n");
}
FreeVec (ColourTable);
}
FreeBitMap (BitMap);
}
if (Locked)
UnlockIBase (IntuiLock);
FreeVec (Pic);
}
FreeVec (RPort);
}
return (NULL);
}
///